home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Turnbull China Bikeride
/
Turnbull China Bikeride - Disc 1.iso
/
HENSA
/
MISC
/
SIOD.ARC
/
!Siod
/
Scm
/
other_scm
next >
Wrap
Text File
|
1993-03-07
|
2KB
|
152 lines
;;; Answers to some questions out of Scheme and the Art of Programming
;;; [Springer & Friedman, MIT Press '89]
;;; Also includes length, first, second, pair and few other bits and bobs
;;;
;;; Hacked in an idle moment by Al Slater, those not marked AmS borrowed 8-)
(define (first l)
(car l)
)
(define (cadar l)
(car (cdr (car l)))
)
(define (second l) ; second item in a list
(car (cdr l))
)
(define (second l) ; tidier
(cadr l)
)
(define (third l)
(caddr l)
)
(define (pair a b)
(cons a (cons b '()))
)
(define (lfy x)
(cons x '())
)
(define (juggle l) ;;; AmS
(cons (third l)
(cons (car l)
(lfy (mysec2 l))
)
)
)
(define (sub1 n)
(- n 1)
)
(define (add1 n)
(+ n 1)
)
(define (length l)
(if (null? l)
0
(add1 (mylen(cdr l))))
)
(define (switch l) ;;;Ams
(cons (third l)
(cons (cadr l) (lfy (car l)))
)
)
(define (mystery ls)
(if (null? (cddr ls))
(cons (car ls) '())
(cons (car ls) (mystery (cdr ls)))
)
)
(define (subst-1st new old ls) ;;; AmS
(if (null? (car ls))
'() ;;; if ls.hd == ()
(if (equal? (car ls) old)
(cons new (cdr ls)) ;;; if found replace new item
;;; otherwise recurse
(cons (car ls) (subst-1st new old (cdr ls)))
)
)
)
(define (fact n)
(if (< n 2)
1
(* n (fact (sub1 n)))
)
)
(define (last l) ;;; AmS
(if (null? (cdr l))
(car l)
(last (cdr l))
)
)
(define (reverse l)
(if (null? l)
'()
(append
(reverse (cdr l))
(list (if (pair? (car l))
(reverse (car l))
(car l)))
)
)
)
(define (singleton? x)
(if (and (pair? x) (null? (cdr x)))
't
'f
)
)
(define (member? x l) ;;; AmS
(if (equal? x (car l))
't
(if (and (null? (cdr l)) (not (equal? x (car l))))
'f
(member? x (cdr l))
)
)
)
(define (mapcar f l) ;;; + AmS Dg
(if (null? (car l))
'()
(cons (f(car l))
(mapcar f (cdr l))
)
)
)
(define (addl l) ;;; + AmS Dg
(if (null? (car l))
0
(+ (car l) (addl (cdr l)))
)
)
(define (nth x l) ;;; AmS
(if (equal? x 1)
(car l)
(nth (- x 1)(cdr l))
)
)